package com.example.HelloWorld;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "LianxiServlet", value = "/LianxiServlet")
public class LianxiServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String username = request.getParameter("username");
        String pwd = request.getParameter("pwd");
        String likes[] = request.getParameterValues("likes");

        //判断传进来的CheckBox是否有值
        if(likes==null){
            request.setAttribute("likes0","暂无爱好");
        }else{
            for (int i = 0; i < likes.length; i++) {
                request.setAttribute("likes" + i, likes[i]);
                System.out.println("likes" + i);

            }
        }

        //System.out.println(Integer.parseInt(request.getParameter("sex")));
        if(request.getParameter("sex")!=null){

            if (Integer.parseInt(request.getParameter("sex"))==0){
                request.setAttribute("sex","男");
            }else {
                request.setAttribute("sex","女");
            }
        }else{
            request.setAttribute("sex","未知");
        }

        String msg = null;
        if(username!=null&&!"".equals(username)&&pwd!=null&&!"".equals(pwd)) {
            //第一种返回方式(请求转发):带着错误提示信息，跳转回到登录页面
            request.setAttribute("username",username);
            request.setAttribute("pwd",pwd);


            request.getRequestDispatcher("success.jsp").forward(request,response);//请求转发
        }else{
            //用户名密码不能为空

            msg = "注册时用户名或密码不能为空";
            request.setAttribute("msg",msg);
            request.getRequestDispatcher("fail.jsp").forward(request,response);
        }




    }
}
